package graduation.project.mutant.strategy;

import graduation.project.body.Expression;
import graduation.project.body.Mutants;
import graduation.project.body.OriginExpression;
import graduation.project.exception.NonExpressionException;

import java.util.ArrayList;

public class LOFMutantsGen implements MutantsGenerator {

	public Mutants generateMutants(String expression, int mutantsNum) {
		
		try{
			OriginExpression originEx = new OriginExpression(expression);
			return generateMutants(originEx, mutantsNum);
		}catch(NonExpressionException e){
			System.out.println("LOFMutantsGen: the expression is invalid.");
			e.printStackTrace();
		}
		return null;
	}

	public Mutants generateMutants(OriginExpression originEx, int num) {
		
		int count = 0;
		String idnf = originEx.getDNF();
		Mutants result = new Mutants(originEx.getExpression(), "LOF");
		ArrayList<Expression> mutantSet = new ArrayList<Expression>();

		StringBuffer mutant = new StringBuffer();
		mutant.append(idnf);
		char literal;
		for(int i = 0; i < idnf.length(); i++){
			literal = idnf.charAt(i);
			try{
				if(literal == '+')
					continue;
				if(literal == '!'){
					literal = idnf.charAt(i + 1);
					mutantSet.add(new Expression(mutant.delete(i, i + 2).toString()));
					mutant.insert(i, "!" + literal);
					i++;
				}else{
					mutantSet.add(new Expression(mutant.deleteCharAt(i).toString()));
					mutant.insert(i, literal);
				}
				if(num != -1 && ++count >= num)
					break;
			}catch(NonExpressionException e){
				System.out.println("the string is not expression");
				e.printStackTrace();
			}			
		}
		
		result.setMutants(mutantSet);
		return result;
	}

	public static void main(String[] args){
		String s = "dgoogd";
		System.out.println(s.split("\\+").length);
	}
}
